package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tongchen
 * @create 2023-02-02 18:47
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增增菜品成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //创建page对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        //创建新的page
        Page<SetmealDto> dtoPage = new Page<SetmealDto>(page,pageSize);
        //创建条件对象
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, queryWrapper);
        //将pageinfo中的变量进行复制到新变量
        BeanUtils.copyProperties(pageInfo,dtoPage,"records" );
        List<Setmeal> records = pageInfo.getRecords();
        //将新变量中的变量重新赋值
        List<SetmealDto>list=records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto );
            //获取categoryid查询categoryname
            Category category = categoryService.getById(item.getCategoryId());
            //避免异常
            if(category!=null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
           return setmealDto;

        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        //返回新对象
        return R.success(dtoPage);
    }
    @DeleteMapping
    public R<String> delete( @RequestParam List<Long>ids){
        log.info(ids.toString());
        setmealService.deleteWithDish(ids);
        return R.success("删除套餐成功");
    }
    @GetMapping("/list")
    public R<List<Setmeal>>list(Setmeal setmeal){
        //通过setmeal查询两个条件
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal!=null, Setmeal::getCategoryId, setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(setmeal!=null, Setmeal::getStatus, setmeal.getStatus());
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(list);
    }
}
